package com.xzj.admin.boot.controller;

import com.xzj.admin.boot.domain.vo.SysRoleVO;
import com.xzj.common.id.gen.DefaultIdHelper;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestBody;
import com.xzj.common.datasource.domain.PageReq;
import cn.hutool.core.util.ObjectUtil;
import com.xzj.common.datasource.domain.PageResp;
import com.xzj.admin.boot.domain.entity.SysRole;
import com.xzj.admin.boot.service.SysRoleService;

import java.util.List;
import java.util.Map;

/**
 * @program: xzj-admin
 * @ClassName: SysRoleController
 * @description: 角色;(sys_role)表控制层
 * @author: XiaZunJun
 * @create: 2024-7-30
 */
@Tag(name = "角色对象功能接口")
@RestController
@RequestMapping("/sysRole")
public class SysRoleController{
    @Resource
    private SysRoleService sysRoleService;

    /**
     * 通过ID查询单条数据
     *
     * @param roleId 主键
     * @return 实例对象
     */
    @Operation(summary = "通过ID查询单条数据")
    @GetMapping("/queryById/{roleId}")
    public SysRole queryById(@PathVariable(name = "roleId")long roleId){
        return sysRoleService.queryById(roleId);
    }

    /**
     * 分页查询
     *
     * @param pageReq 分页筛选条件对象
     * @return 查询结果
     */
    @Operation(summary = "分页查询")
    @PostMapping("/pageQuery")
    public PageResp<SysRoleVO> pageQuery(@RequestBody PageReq<SysRoleVO> pageReq){
        return sysRoleService.pageQuery(pageReq);
    }


    @Operation(summary = "查询角色map")
    @GetMapping("/roleMap")
    public Map<String,String> roleMap(){
        return sysRoleService.roleMap();
    }
    /**
     * 新增数据
     *
     * @param sysRole 实例对象
     * @return 实例对象
     */
    @Operation(summary ="新增Or更新数据")
    @PostMapping("/addOrUpdate")
    public SysRoleVO addOrUpdate(@RequestBody SysRoleVO sysRole){
        if(ObjectUtil.isNotEmpty(sysRole.getRoleId())){
            sysRoleService.update(sysRole);
        }else{
            sysRole.setRoleId(DefaultIdHelper.nextIdStr());
            sysRoleService.insert(sysRole);
        }
        return sysRole;
    }

    /**
     * 通过主键删除数据
     *
     * @param roleId 主键
     * @return 是否成功
     */
    @Operation(summary ="通过主键删除数据")
    @GetMapping("/deleteById/{roleId}")
    public boolean deleteById(@PathVariable(name = "roleId") long roleId){
        return sysRoleService.deleteById(roleId);
    }

    /**
     * 查询所有角色
     *
     * @return 是否成功
     */
    @Operation(summary ="查询所有角色")
    @GetMapping("/queryAllRoleList")
    public List<SysRole> queryAllRoleList(SysRole sysRole){
        return sysRoleService.queryAllRoleList(sysRole);
    }

}